﻿using Single.Logger;
using Single.Sugar.Service;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Single.Sys.Service
{
    public class SysLogService : SugarService
    {
        public List<LoggerEntity> EntityGetPageList(int pageNum, int pageSize, DateTime start, DateTime end, string type, string status, string user, string message, string data, out int total)
        {
            var query = db.Queryable<LoggerEntity>().Where(m => SqlFunc.Between(m.CreateTime, start, end.AddDays(1)));

            query = query.WhereIF(!string.IsNullOrEmpty(type), m => m.LogType == type)
                .WhereIF(!string.IsNullOrEmpty(status), m => m.LogStatus == status)
                .WhereIF(!string.IsNullOrEmpty(user), m => m.UserId.ToString() == user || m.Username == user || m.Name == user)
                .WhereIF(!string.IsNullOrEmpty(message), m => m.Message.Contains(message))
                .WhereIF(!string.IsNullOrEmpty(data), m => m.Data.Contains(data));

            return query.OrderByDescending(m => m.CreateTime).ToPageList(pageNum, pageSize, out total);
        }

        public List<LoggerRequest> RequestGetPageList(int pageNum, int pageSize, DateTime start, DateTime end, string method, string url, string ip, string browser, string contentType, string body, string user, out int total)
        {
            var query = db.Queryable<LoggerRequest>().Where(m => SqlFunc.Between(m.CreateTime, start, end.AddDays(1)));

            query = query.WhereIF(!string.IsNullOrEmpty(method), m => m.Method == method)
                .WhereIF(!string.IsNullOrEmpty(url), m => m.Url.Contains(url))
                .WhereIF(!string.IsNullOrEmpty(ip), m => m.UserIp.Contains(ip))
                .WhereIF(!string.IsNullOrEmpty(browser), m => m.BrowserType.Contains(browser))
                .WhereIF(!string.IsNullOrEmpty(contentType), m => m.ContentType.Contains(contentType))
                .WhereIF(!string.IsNullOrEmpty(body), m => m.RequestParameter.Contains(body))
                .WhereIF(!string.IsNullOrEmpty(user), m => m.UserId.ToString() == user || m.Username == user || m.Name == user);

            return query.OrderByDescending(m => m.CreateTime).ToPageList(pageNum, pageSize, out total);
        }
    }
}